package com.uchedao.seller.ui.carlist.province_city;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.uchedao.seller.R;
import com.uchedao.seller.ui.carlist.BaseActivity;
import com.uchedao.seller.ui.carlist.db.CarDbHelper;
import com.uchedao.seller.ui.carlist.entity.CityEntity;
import com.uchedao.seller.ui.carlist.entity.LocationEntity;
import com.uchedao.seller.ui.carlist.entity.ProvinceEntity;

import java.util.ArrayList;

/**
 * 城市选择器
 * Created by ZhangZhanghua on 2015/10/29 0029.
 */
public class ProvinceCityActivity extends BaseActivity implements View.OnClickListener {
    public static final String EXTRA_LOCATION = "extra_location";
    private ListView provinceView;
    private ListView cityView;
    private ProvinceAdapter provinceAdapter;
    private CityAdapter cityAdapter;
    private ArrayList<ProvinceEntity> provinceList;
    private ArrayList<CityEntity> cityList;
    private int provinceId, cityId;
    private String provinceName, cityName;
    private LinearLayout container;
    @Override
    protected String getActivityTag() {
        return "ProvinceCityActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_province_city;
    }

    @Override
    public void initListener() {
        final CityEntity cityEntity = new CityEntity();
        cityEntity.setId(0);
        cityEntity.setName("不限城市");
        provinceView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                provinceId = provinceList.get(position).getId();
                provinceName = provinceList.get(position).getName();

                Log.e("tag", provinceName);
                cityList = CarDbHelper.getCity(ProvinceCityActivity.this, provinceId);
                cityList.add(0, cityEntity);
                cityAdapter = new CityAdapter(ProvinceCityActivity.this, cityList, provinceName);
                container.setVisibility(View.VISIBLE);
                cityView.setAdapter(cityAdapter);
                cityAdapter.notifyDataSetChanged();
            }
        });
        cityView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityId = cityList.get(position).getId();
                cityName = cityList.get(position).getName();
//                Bundle bundle = new Bundle();
//                ArrayList ids = new ArrayList();
//                ArrayList name = new ArrayList();
                LocationEntity entity = new LocationEntity(cityId, cityName, provinceId, provinceName);
//                ids.add(provinceId);
//                ids.add(cityId);
//                name.add(provinceName);
//                name.add(cityName);
//                bundle.putStringArrayList("ID", ids);
//                bundle.putStringArrayList("NAME", name);
                Intent intent = new Intent();
                intent.putExtra(EXTRA_LOCATION, entity);
//                intent.putExtras(bundle);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        provinceView = (ListView) findViewById(R.id.province_list);
        cityView = (ListView) findViewById(R.id.city_list);
        container = (LinearLayout) findViewById(R.id.city_view_container);
        TextView tv = (TextView) findViewById(R.id.title_back);
        tv.setText("返回");
        tv.setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.tv_title_name);
        title.setText("所在地");
        provinceList = CarDbHelper.getProvince(ProvinceCityActivity.this);
        provinceAdapter = new ProvinceAdapter(ProvinceCityActivity.this, provinceList);
        provinceView.setAdapter(provinceAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }
}